package com.tanhua.dubbo.api;

import cn.hutool.core.collection.CollUtil;
import com.tanhua.model.mongo.UserLike;
import com.tanhua.model.vo.UserLikeVo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.List;

@DubboService
public class UserLikeApiImpl implements UserLikeApi {
    @Autowired
    private MongoTemplate mongoTemplate;
/**
 *     private Long userId; //用户id
 *     private Long likeUserId; //喜欢的用户id
 *     private Boolean isLike; // 是否喜欢
 *     private Long created; //创建时间
 *     private Long updated; // 更新时间
 */

    @Override
    public UserLikeVo counts(Long userId) {

        //互相喜欢
        Query query = Query.query(Criteria.where("userId")
                .is(userId)
                .and("isLike")
                .is(true));
        List<UserLike> list = mongoTemplate.find(query, UserLike.class);
        //提取出用户喜欢的用户id
        List<Long> userLikeIds = CollUtil.getFieldValues(list, "likeUserId", Long.class);
        Query query1 = Query.query(Criteria.where("userId").in(userLikeIds).and("likeUserId").is(userId));
        long count1 = mongoTemplate.count(query1, UserLike.class);

        //我喜欢的
        Query query2 = Query.query(Criteria.where("isLike")
                .is(true)
                .and("userId")
                .is(userId));
        long count2 = mongoTemplate.count(query2, UserLike.class);

        //我的粉丝
        Query query3 = Query.query(Criteria.where("isLike")
                .is(true)
                .and("likeUserId")
                .is(userId));
        long count3 = mongoTemplate.count(query3, UserLike.class);

        UserLikeVo vo = new UserLikeVo();
        vo.setEachLoveCount((int) count1);
        vo.setLoveCount((int) count2);
        vo.setFanCount((int) count3);
        return vo;
    }
    @Override
    //互相喜欢列表
    public List<UserLike> findLove(Integer page, Integer pagesize, Long userId) {
        Query query = Query.query(Criteria.where("userId").is(userId).and("isLike").is(true));
        List<UserLike> list = mongoTemplate.find(query, UserLike.class);
        List<Long> userLikeIds = CollUtil.getFieldValues(list, "likeUserId", Long.class);
        Query query1 = Query.query(Criteria.where("userId").in(userLikeIds)
                .and("likeUserId").is(userId));
        return mongoTemplate.find(query1, UserLike.class);
    }
    @Override
    //我喜欢别人列表
    public List<UserLike> findILove(Integer page, Integer pagesize, Long userId) {
        Query query = Query.query(Criteria.where("userId").is(userId)
                .and("isLike").is(true));
        return mongoTemplate.find(query, UserLike.class);
    }
    @Override
    //别人喜欢我的列表
    public List<UserLike> findLoveI(Integer page, Integer pagesize, Long userId) {
        Query query = Query.query(Criteria.where("likeUserId").is(userId)
                .and("isLike").is(true));
        return mongoTemplate.find(query, UserLike.class);
    }
    @Override
    //判断我是否喜欢ta
    public boolean iLove(Long userId, Long likeUserId) {
        Query query = Query.query(Criteria.where("likeUserId").is(likeUserId)
                .and("userId").is(userId)
                .and("isLike").is(true));
        return mongoTemplate.exists(query, UserLike.class);
    }
    //关注
    @Override
    public Boolean saveOrUpdate(Long userId, Long likeUserId, boolean isLike) {

        try {
            //保存数据到数据库
            //1、查询数据
            Query query = Query.query(Criteria.where("userId").is(userId).and("likeUserId").is(likeUserId));
            UserLike userLike = mongoTemplate.findOne(query, UserLike.class);
            //2、如果不存在，保存
            if(userLike == null) {
                userLike = new UserLike();
                userLike.setUserId(userId);
                userLike.setLikeUserId(likeUserId);
                userLike.setCreated(System.currentTimeMillis());
                userLike.setUpdated(System.currentTimeMillis());
                userLike.setIsLike(isLike);
                mongoTemplate.save(userLike);
            }else {
                //3、更新
                Update update = Update.update("isLike", isLike)
                        .set("updated",System.currentTimeMillis());
                mongoTemplate.updateFirst(query,update,UserLike.class);
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
    /**
     * 查询是否是双向喜欢
     */
    @Override
    public Boolean TwoWayRush(Long userId, Long likeUserId, boolean b) {
        Query query1 = Query.query(Criteria.where("userId").is(userId).and("likeUserId").is(likeUserId).and("isLike").is(b));
        UserLike one = mongoTemplate.findOne(query1, UserLike.class);

        Query query2 = Query.query(Criteria.where("userId").is(userId).and("likeUserId").is(likeUserId).and("isLike").is(b));
        UserLike two = mongoTemplate.findOne(query2, UserLike.class);
        return one != null && two != null;

    }
    /**
     * 删除userLike表中喜欢的数据
     */
    @Override
    public Boolean deleteById(Long userId, Long unLikeUserId, boolean b) {
        try {
            Query query = Query.query(Criteria.where("userId")
                    .is(userId).and("likeUserId").is(unLikeUserId).and("isLike").is(b));
            mongoTemplate.remove(query,UserLike.class);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }

    }


}


